package be.kuleuven.mume.listProxys;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

public class FirstCellLoadingProxy extends LastCellLoadingProxy{

	public FirstCellLoadingProxy(ListAdapter adapter) {
		super(adapter);
	}

	@Override
	public Object getItem(int position){
		if(position == 0 && this.state.equals(State.Loading))
			return null;
		assert position < adapter.getCount();
		return adapter.getItem(position);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if(getItemViewType(position) == LOADING_TYPE) {
			assert position == 0;
			return getRowView(convertView, LOADING_ID, parent);
		}
		if(getItemViewType(position) == NO_DATA_TYPE){
			assert position == 0;
			return getRowView(convertView, NO_DATA_ID, parent);
		}
		if(state.equals(State.Loading))
			position--;
		assert !(position < 0);
		assert (position < adapter.getCount());
		return adapter.getView(position, convertView, parent);
	}
	
	@Override
	public long getItemId(int position) {
		if(state.equals(State.Loading) && position == 0)
			return 0;
		if(state.equals(NO_DATA_ID)) {
			assert position == 0;
			return 1;
		}
		return position + 2;
	}
	
	@Override
	public int getItemViewType(int position) {
		
		if(this.state.equals(State.Loading) ) {
			if( position == 0)
				return LOADING_TYPE;
			position--;
		}
		else if(this.state.equals(State.NoData)) {
			assert position == 0;
			return NO_DATA_TYPE;
		}
		assert (position < adapter.getCount());
		return countTypes + adapter.getItemViewType(position);
	}
	
	@Override
	public boolean isEnabled(int position) {
		if(state.equals(State.Loading)) {
			if(position == 0)
				return false;
			position--;
		}
		return adapter.isEnabled(position);
	}
}
